package com.prime.tutorial.moviestore.view.techsupport;

import com.prime.tutorial.moviestore.domain.Device;
import com.prime.tutorial.moviestore.service.techsupport.TechSupportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.faces.component.html.HtmlDataTable;
import javax.faces.event.ActionEvent;
import java.io.Serializable;
/**
 * Responsible for techSupport device installation.
 * Provides the dataTable for the binding with the list of devices from ListDevicesForInstallation and
 * provides the link to the service layer of techSupport.
 * @author vasylja
 */

@Component("installDevice")
@Scope("request")
public class InstallDevice implements Serializable {
    private TechSupportService techSupportService;
//    private HtmlDataTable dataTable;
    private Device[] arrayDeviceToInstall;
    
    @Autowired
    public InstallDevice(TechSupportService techSupportService) {
        setTechSupportService(techSupportService);
    }

//    public HtmlDataTable getDataTable() {
//        return dataTable;
//    }
//
//    public void setDataTable(HtmlDataTable dataTable) {
//        this.dataTable = dataTable;
//    }

    private TechSupportService getTechSupportService() {
        return techSupportService;
    }

    private void setTechSupportService(TechSupportService techSupportService) {
        this.techSupportService = techSupportService;
    }

//    public String installDevice(){
//        techSupportService.installDevice((Device)dataTable.getRowData());
//        return "installDevice";
//    }

    public void installDevice(ActionEvent e){
        for(int i=0;i<getArrayDeviceToInstall().length;i++){
            techSupportService.installDevice(getArrayDeviceToInstall()[i]);
        }
        setArrayDeviceToInstall(null);
    }

    public Device[] getArrayDeviceToInstall() {
        return arrayDeviceToInstall;
    }

    public void setArrayDeviceToInstall(Device[] arrayDeviceToInstall) {
        this.arrayDeviceToInstall = arrayDeviceToInstall;
    }
}
